<?php
require_once("ITransformer.php");

/**
 * Transforms the given EmbraceModel into PHP Interface.
 * 
 * @author s.marletta
 * @version $Id: InterfaceTransformer.php 6 2009-04-28 20:11:07Z embrace.toolkit $
 */
class InterfaceTransformer implements ITransformer {
	/**
	 * Contains the path usedfor interfaces.
	 * 
	 * @var String
	 */
	private $path;
	
	/**
	 * Filename of the interface result.
	 * @var String
	 */
	private $fileName;
	
	/**
	 * Interface name for code.
	 * @var String
	 */
	private $interfaceName;
	
	/**
	 * the used model.
	 * @var EmbraceModel
	 */
	private $data;
	
	/**
	 * (non-PHPdoc)
	 * @see embrace/lib/ITransformer#transform($xml)
	 */
	public function transform($data) {
		if($data == null || empty($data))
			throw new Exception("Model must be given.");
		
		$this->data = $data;
		$this->path = "output" . $this->data->getSettings()->getSetting("service_path") . 
			"/php";
		
		if(!mkdirRecursive($this->path, 0777))
			throw new Exception("Unable to create interface directory.");
		
		$this->interfaceName = "I" . $this->data->getName() ; 
		$this->fileName = $this->interfaceName . ".php";
		
		$fHandle = fopen($this->path . "/" . $this->fileName, "w+");
		
		if(!is_resource($fHandle))
			throw new Exception("Unable to create Interface file.");
		
		$this->writeFileComment($fHandle);
		$this->writeInterfaceDescriptor($fHandle);
	
		$buffer = "}" . self::delimiter;
		$buffer .= "?>" . self::delimiter;
		fwrite($fHandle, $buffer);
	}
	
	/**
	 * writes the database statemnts into the interface.
	 * 
	 * @param $fHandle file handle for the interface file.
	 */
	protected function writeDatabaseConstants(&$buffer) {
		$buffer .= "\t/**" . self::delimiter;
		$buffer .= "\t * Select SQL for the models data." . self::delimiter;
		$buffer .= "\t */" . self::delimiter;
		$buffer .= "\tconst SELECT_SQL = \"SELECT ";
		
		$arrFields = $this->data->getFields();
		foreach($arrFields as $field) {
			$buffer .= $field->getFieldtable() . "." . $field->getFieldName() . ",";
		}
		$buffer = substr($buffer, 0, -1);
		
		$buffer .= " FROM ";
		
		$arrTables = $this->data->getTables();
		foreach($arrTables as $table) {
			$buffer .= $table->getTableName() . ",";
		}
		
		$buffer = substr($buffer, 0, -1);
		$buffer .= " WHERE " . "\";" . self::delimiter;
		$buffer .= self::delimiter;
	}
	
	/**
	 * Writes the interface descriptor.
	 * 
	 * @param $fHandle
	 * @return unknown_type
	 */
	protected function writeInterfaceDescriptor($fHandle) {
		$buffer = "interface " . $this->interfaceName . " {" . self::delimiter;
		$buffer .= "\t/**" . self::delimiter;
		$buffer .= "\t * Property is generated by Embrace and used for validating" . self::delimiter;
		$buffer .= "\t * PHP Models against there JS Models and data providing service." . self::delimiter;
		$buffer .= "\t *" . self::delimiter;
		$buffer .= "\t * @var String" . self::delimiter;
		$buffer .= "\t */" . self::delimiter;
		$buffer .= "\tconst GUID = \"" . 
			$this->data->getParameters()->getParameter("validate") . 
			"\";" . self::delimiter;
		$buffer .= self::delimiter;
		
		$this->writeDatabaseConstants(&$buffer);
				
		$arrayMethods = $this->data->getMethods();
		
		if($arrayMethods === false)
			throw new Exception("No methods contained.");
		
		foreach($arrayMethods as $method) {
			$buffer .= "\t/**" . self::delimiter;
			$buffer .= "\t * From model generated method." . self::delimiter;
			$buffer .= "\t * Type: " . $method->getMethodType() . self::delimiter;
			$buffer .= "\t */" . self::delimiter;
			$buffer .= "\tpublic function " . $method->getMethodName() . "();" . self::delimiter;
			$buffer .= self::delimiter;
		}
		
		fwrite($fHandle, $buffer);
	}
	
	/**
	 * Writes the class comment to the file output.
	 * 
	 * @param $fHandle
	 */
	protected function writeFileComment($fHandle) {
		$buffer = "<?php" . self::delimiter;
		$buffer .= "/**" . self::delimiter;
		$buffer .= " * " . $this->interfaceName . self::delimiter;
		$buffer .= " *" . self::delimiter;
		$buffer .= " * This interface is created by using XML data model description" . self::delimiter;
		$buffer .= " * and compiled by Embrace Modelling for PHP/JS." . self::delimiter;
		$buffer .= " *" . self::delimiter;
		$buffer .= " * Do not change the interface description, when regenerating with" . self::delimiter;
		$buffer .= " * Embrace all changes will be loosed." . self::delimiter;
		$buffer .= " *" . self::delimiter;
		$buffer .= " * @author Embrace Modelling for PHP/JS" . self::delimiter;
		$buffer .= " * @version \$Id: InterfaceTransformer.php 6 2009-04-28 20:11:07Z embrace.toolkit $" . self::delimiter;
		$buffer .= "*/" . self::delimiter;
				
		fwrite($fHandle, $buffer);
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	public function createReport() {
		
	}
}
?>